package com.jz.yanshe.ui;

import android.graphics.Paint;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.jz.yanshe.base.BaseActivity;

import com.jz.jd.rest.business.RestProxy;
import com.jz.jd.rest.response.GoodsDetailResponse;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import com.jz.yanshe.dialog.BuyDialog;
import com.jz.yanshe.util.ImageLoaderHelper;
import com.jz.yanshe.util.PreferencesHelper;

import com.jz.yanshe.R;
/**
 * Created by Administrator on 2018/2/27.
 */

public class GoodsDetailActivity extends BaseActivity {

    private BuyDialog mBuyDialog;

    @BindView(R.id.banner)
    RecyclerView banner;

    @BindView(R.id.tv_goods_name)
    TextView tv_goods_name;

    @BindView(R.id.tv_price_now)
    TextView tv_price_now;

    @BindView(R.id.tv_price)
    TextView tv_price;

    @BindView(R.id.tv_sales_volume)
    TextView tv_sales_volume;

    @BindView(R.id.tv_size)
    TextView tv_size;

    @BindView(R.id.webview)
    WebView webview;

    List<String> list = new ArrayList<>();
    String goodsid;
    GoodsDetailResponse goodsDetailResponse;
    MyAdapter myAdapter = new MyAdapter();


    @Override
    protected void initViews() {
        tv_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        WebSettings settings = webview.getSettings();
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        banner.setAdapter(myAdapter);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        banner.setLayoutManager(manager);
    }

    @Override
    protected void initData() {
        goodsid = getIntent().getStringExtra("goodsid");
        getDetail(goodsid);
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_goodsdetail;
    }

    @OnClick(R.id.btn_size)
    void btn_size() {
        showBuyDialog("");
    }



    @OnClick(R.id.btn_add_to_shoppingbag)
    void btn_add_to_shoppingbag() {
        showBuyDialog("");
    }




    @OnClick(R.id.btn_back)
    void btn_back() {
        finish();
    }


    private void showBuyDialog(final String id) {
        if (mBuyDialog == null) {
            mBuyDialog = mBuyDialog.newInstance();
            mBuyDialog.show(getSupportFragmentManager(), "mTextDialogFragment");
//            mBuyDialog.setmListener(new mBuyDialog.OnItemClickListener() {
//                @Override
//                public void onConfirm(String name) {

//                }
//            });
        } else if (!mBuyDialog.isAdded() && !mBuyDialog.isShowing()) {
            mBuyDialog.show(getSupportFragmentManager(), "mAgeDialogFragment");
        }
        mBuyDialog.getAllSize(goodsid);
        mBuyDialog.goodsid = goodsid;
        mBuyDialog.setData(goodsDetailResponse.map.goods_price_now, goodsDetailResponse.map.goods_inventory, goodsDetailResponse.map.goods_icon);
    }


    private void getDetail(String good_id) {
        RestProxy.getInstance().getGoodsDetail(PreferencesHelper.getInstance().getString(this,"token"),good_id, new Observer<GoodsDetailResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(GoodsDetailResponse goodsDetailResponse) {
                tv_goods_name.setText(goodsDetailResponse.map.goods_name);
                tv_price_now.setText("¥" + goodsDetailResponse.map.goods_price_now);
                tv_price.setText("¥" + goodsDetailResponse.map.goods_price);
                tv_sales_volume.setText("已售" + goodsDetailResponse.map.sales_volume + "件");
                list.clear();
                for (int i = 0; i < goodsDetailResponse.map.goods_imgs.split(",").length; i++) {
                    list.add(goodsDetailResponse.map.goods_imgs.split(",")[i]);
                }
                myAdapter.notifyDataSetChanged();
//                banner.setImages(list).setBannerStyle(BannerConfig.CIRCLE_INDICATOR)
//                        .setBannerAnimation(Transformer.Tablet).setImageLoader(new ImageLoader() {
//                    @Override
//                    public void displayImage(Context context, Object path, ImageView imageView) {
//                        ImageLoaderHelper.getInstance().GlideImageLoader(context, (String) path, imageView);
//                    }
//                }).start();
                String data = "<HTML><Div align=\"center\"  margin=\"0px\"><meta name=\"viewport\" charset=\"utf-8\" content=\"width=device-width,\n" +
                        " initial-scale=1.0, maximum-scale=1.0, user-scalable=no\"/><style type=\"text/css\">img{width:100%;object-fit:cover;object-position:center;}</style>"+goodsDetailResponse.map.goods_detail +"</Div>";
                webview.loadDataWithBaseURL(null,data, "text/html", "UTF-8",null);
                GoodsDetailActivity.this.goodsDetailResponse = goodsDetailResponse;
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }



    class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder> {


        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            Holder holder = new Holder(getLayoutInflater().inflate(R.layout.item_imageview, parent, false));
            return holder;
        }

        @Override
        public void onBindViewHolder(Holder holder, final int position) {
            ImageLoaderHelper.getInstance().GlideImageLoader(GoodsDetailActivity.this,list.get(position),holder.imageView);
        }


        @Override
        public int getItemCount() {
            return list.size();
        }

        class Holder extends RecyclerView.ViewHolder {
        ImageView imageView;
            public Holder(View itemView) {
                super(itemView);
                imageView = itemView.findViewById(R.id.img1);
            }
        }
    }

}
